/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-06
 * Time: 19:46
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;

using tools;
using patterns;

using Labb4.Main;
using Labb4.Media;
using Labb4.Media.Graphics;
using Labb4.Pooman.Misc;
using Labb4.Pooman.Game;

namespace Labb4.Entity
{
	/// <summary>
	/// Creates picture entities from the information found in a config file pointed
	/// to by a string.
	/// 
	/// Implements the Factory Method design pattern
	/// </summary>
	public sealed class PictureEntityFactory : FactoryMethod<BasicEntity>
	{
		#region overrides
		
		/// <summary>
		/// Creates and returns a picture entity. To create the entity, information is used
		/// found in a file.
		/// </summary>
		/// <param name="configFile">The name and address of a file storing information about the picture
		/// entity to create.</param>
		/// <returns>The created sprite entity</returns>
		protected override BasicEntity CreateProduct(string configFile)
		{
			BasicEntity pictureEntity = null;
			Config config = new Config();
			
			if(MainForm.ErrorCheck(config.Open(configFile)))
			{
				string pictureFile = "";
				List<int> colorKey = new List<int>();
				int x = 0;
				int y = 0;
				float dir = 0.0f;
				float vel = 0.0f;
				
				if(MainForm.ErrorCheck(config.GetString("picture_file", ref pictureFile)) &&
				   MainForm.ErrorCheck(config.GetIntList("color_key_mask", colorKey, false)) &&
				   MainForm.ErrorCheck(config.GetInt("entity_x", ref x)) &&
				   MainForm.ErrorCheck(config.GetInt("entity_y", ref y)) &&
				   MainForm.ErrorCheck(config.GetFloat("entity_dir", ref dir)) &&
				   MainForm.ErrorCheck(config.GetFloat("entity_vel", ref vel)))
				{
					Picture picture = MediaManager.Instance.GetPicture(Properties.Instance.StartPath + pictureFile);
					
					if(colorKey.Count == 3)
					{
						picture.SetTransparentColor(colorKey[0], colorKey[1], colorKey[2]);
					}
					
					pictureEntity = new PictureEntity(picture, (float)x, (float)y, picture.ImageData.Width,
					                                  picture.ImageData.Height, vel, dir);
				}
			}
			
			return pictureEntity;
		}
		
		/// <summary>
		/// Does nothing, but the class this class is derived from forces this class to implement this
		/// method.
		/// </summary>
		/// <returns>null</returns>
		protected override BasicEntity CreateProduct()
		{
			return null;
		}
		
		#endregion
	}
}
